package cn.tedu.exam.controller;


import cn.tedu.exam.common.Result;
import cn.tedu.exam.model.DeviceInfo;
import cn.tedu.exam.service.IDeviceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器111
 * </p>
 *
 * 设备信息表
 *
 * @author admin
 * @since 2022-01-07
 */
@Slf4j
@RestController
@RequestMapping("/exam/deviceInfo")
public class DeviceInfoController {

    @Autowired
    private IDeviceInfoService iDeviceInfoService;

    @Value("${server.port}")
    private String port;

    /**
     * 更新设备信息
     *
     * @param info
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody DeviceInfo info) {
        log.info("设备更新--：" + info.toString());
        /**
         *
         */
        boolean f = iDeviceInfoService.updateById(info);
        if (f) {
            return Result.ok("操作成功！");
        } else {
            return Result.error("操作失败！");
        }
    }


    public String prin(){
        return "/login.html";
    }


    /**
     * 移除设备
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("设备移除--：" + id);
        boolean f = iDeviceInfoService.removeById(id);
        if (f) {
            return Result.ok("操作成功！");
        } else {
            return Result.error("操作失败！");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch/{ids}")
    public Result deleteBatch(@PathVariable String ids) {
        log.info("批量删除--:" + ids);
        String[] list = ids.split(",");
        boolean f = iDeviceInfoService.deleteBatch(list);
        if (f) {
            return Result.ok("操作成功！");
        } else {
            return Result.error("操作失败！");
        }
    }


    /**
     * 设备入库
     * @param model
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody DeviceInfo model) {
        log.info("设备入库--:" + model.toString());
        boolean f = iDeviceInfoService.add(model);
        if (f){
            return Result.ok("操作成功！");
        }else{
            return Result.error("操作失败！");
        }
    }

    /**
     * 设备详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getOne(@PathVariable Long id) {
        log.info("设备详情--:" + id);
        DeviceInfo pojo = iDeviceInfoService.getById(id);
        return Result.ok(pojo);
    }

    /**
     * 设备出库
     * （修改设备信息所在地）
     * @return
     */
    @GetMapping("/modify/{id}/{location}")
    public Result modify(@PathVariable Long id ,@PathVariable String location){
        log.info("设备出库--：{}，{}", id, location);
        boolean f = iDeviceInfoService.modify(id,location);
        if (f){
            return Result.ok("操作成功！");
        }else{
            return Result.error("操作失败！");
        }
    }

    /**
     * 设备回收
     * （将设备所在地删除，将状态改为在库）
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result back(@PathVariable Long id){
        log.info("设备收回--:" + id);
        boolean f = iDeviceInfoService.back(id);
        if (f){
            return Result.ok("操作成功！");
        }else{
            return Result.error("操作失败！");
        }
    }

    /**
     * 查询设备列表
     * @return
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum,
                       @PathVariable Integer pageSize,
                       DeviceInfo req) {

        log.info("查询设备列表--：" + req.toString());
        log.info("页码信息--：" + pageNum + "页，每页" + pageSize + "条");
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<DeviceInfo> wrapper = new QueryWrapper<>();
        if (req.getDeviceName() != null && !"".equals(req.getDeviceName())) {
            wrapper.like("device_name", req.getDeviceName());
        }
        if (req.getDeviceModel() != null && !"".equals(req.getDeviceModel())) {
            wrapper.like("device_model", req.getDeviceModel());
        }
        if (req.getBrand() != null && !"".equals(req.getBrand())) {
            wrapper.like("brand", req.getBrand());
        }
        if (req.getStatus() != null && req.getStatus() != 0) {
            wrapper.eq("status", req.getStatus());
        }
        if (req.getLocation() != null && !"".equals(req.getLocation())) {
            wrapper.like("location", req.getLocation());
        }
        if (req.getType() != null && !"".equals(req.getType())) {
            wrapper.eq("type", req.getType());
        }
        List<DeviceInfo> list = iDeviceInfoService.list(wrapper);
        PageInfo<DeviceInfo> pageInfo = new PageInfo<>(list);
        return Result.ok(pageInfo);
    }


}
